package snmp;

import com.ruoyi.common.snmp.getSnmpf.GetInformation;
import com.ruoyi.common.snmp.ip.Ping;
import com.ruoyi.system.domain.SnmpSystem;
import com.ruoyi.system.service.ISnmpSystemService;
import org.snmp4j.CommunityTarget;
import org.snmp4j.PDU;
import org.snmp4j.Snmp;
import org.snmp4j.TransportMapping;
import org.snmp4j.event.ResponseEvent;
import org.snmp4j.mp.SnmpConstants;
import org.snmp4j.smi.GenericAddress;
import org.snmp4j.smi.OID;
import org.snmp4j.smi.OctetString;
import org.snmp4j.smi.VariableBinding;
import org.snmp4j.transport.DefaultUdpTransportMapping;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;


public class test {
    private ISnmpSystemService snmpSystemService;
    public static void main(String[] args) throws Exception {
        //testNetSnmp();

        List<SnmpSystem> deviceIPS= new ArrayList<>();
        SnmpSystem obj=new SnmpSystem();
        //obj.setIp("192.168.50.22");
        //obj.setIp("192.168.0.1");
        //obj.setIp("192.168.50.219");
        obj.setIp("192.168.123.2");
        //obj.setIp("192.168.3.102");
       // obj.setIp("192.168.1.20");
        deviceIPS.add(obj);
        for (SnmpSystem item:deviceIPS
             ) {
            System.out.println("正在ping：" + item.getIp());
            if (Ping.ping(item.getIp())) {
                System.out.println("  ........   ........ping success");
            }
            if (Ping.GetSnmpSuccess(item.getIp())) {
               GetInformation.checkSnmpVersion(item.getIp(),161);
                GetInformation.getBaseInfo(item.getIp());

                GetInformation.setBaseInfo(item.getIp());
               //获得接入点  的信道宽度 发送功率
              GetInformation.getCpuInfo(item.getIp());
                //GetInformation.getDiskInfo(item.getIp());//重复getMemoryInfo

                GetInformation.getInterface(item.getIp());
  /*               GetInformation.getMemoryInfo(item.getIp());
                GetInformation.getPort(item.getIp());
                GetInformation.getProcess(item.getIp());
                GetInformation.getSoft(item.getIp());
                GetInformation.getService(item.getIp());
*/

            }
        }
    }

    public static void testNetSnmp()
    {
        // 创建传输映射 (UDP)
        TransportMapping transport = null ;
        try {
            transport = new DefaultUdpTransportMapping();
            transport.listen();
        } catch (IOException e) {
            System.err.println("Error opening port: " + e.getMessage());
            return;
        }

        // 创建 SNMP 对象
        Snmp snmp = new Snmp(transport);

        // 创建目标地址
        CommunityTarget target = new CommunityTarget();
        target.setCommunity(new OctetString("public")); // 社区字符串
        target.setAddress(GenericAddress.parse("udp:192.168.123.2/161")); // SNMP 服务器地址和端口
        target.setRetries(2);
        target.setTimeout(1500); // 超时时间
        target.setVersion(SnmpConstants.version2c); // 使用 SNMP 版本 2c

        // 创建 PDU (Protocol Data Unit)
        PDU pdu = new PDU();
        //.1.3.6.1.2.1.4.22.1.3.0
        pdu.add(new VariableBinding(new OID("1.3.6.1.2.1.1.1.0"))); // sysDescr.0
        pdu.setType(PDU.GET);

        // 发送请求并接收响应
        ResponseEvent response = null;
        try {
            response = snmp.send(pdu, target);
        } catch (IOException e) {
            System.err.println("Error sending request: " + e.getMessage());
        }

        if (response != null && response.getResponse() != null) {
            PDU responsePDU = response.getResponse();
            for (VariableBinding vb : responsePDU.getVariableBindings()) {
                System.out.println(vb.getOid() + " = " + vb.getVariable());
            }
        } else {
            System.out.println("No response received.");
        }

        try {
            snmp.close();
        } catch (IOException e) {
            System.err.println("Error closing SNMP: " + e.getMessage());
        }
    }
}
